/**
 * JourneyOptionView Class
 * 
 * This class is the menu JPanel class responsible for displaying all of the options
 * and information about the user and the information that is not provided by the 
 * JourneyView class. 
 * 
 * GUI Notes for Team:
 * The GUI layout that I am using for this class is called a Java "Absolute Layout." 
 * This means that I can add element anywhere that I want to on JPanels. Just so we 
 * are all on the same page, I am able to manipulate the location of each GUI element 
 * by the following call: itemname.setBounds(x location, y location, width, height);
 * I have labeled all of the areas in this file where I have set the location, but 
 * if you have any questions, feel free to give me a call. 
 */
package edu.gatech.cs2340.bytebankers.view;

import javax.swing.*;

import edu.gatech.cs2340.bytebankers.presenter.IJourneyOptionPresenter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class JourneyOptionView extends JPanel implements IJourneyOptionView {

    private int ration;
    private int pace;
    private String paceString;
    private int foodLeft, day, year;
    public JButton btnContinue;
    public JButton btnMao;
    private IJourneyOptionPresenter presenter;
    private String[] milesArray = new String[] { "Very Slow", "Slow", "Medium",
	    "Fast", "Very Fast" };
    private String[] rationArray = new String[] { "1", "2", "3", "4" };
    private JLabel lblFood, lblFood2, distLabel, foodLeftLabel;
    JComboBox rationbox = new JComboBox();
    JComboBox milesbox = new JComboBox();
    private double bankroll;
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    NumberFormat df = new DecimalFormat("#.##");
    private double distanceTravelled;
    private double distance;

    public JourneyOptionView(IJourneyOptionPresenter presenter) {
	this.presenter = presenter;

	setLayout(null);
	/**
	 * Main Label for the Options Menu
	 */
	JLabel lblMenu = new JLabel("Menu");
	lblMenu.setForeground(Color.WHITE);
	lblMenu.setFont(new Font("Lucida Grande", Font.BOLD, 18));
	lblMenu.setBounds(405, 17, 51, 16);
	add(lblMenu);

	/**
	 * Options below with JLabels
	 */
	JLabel lblDate = new JLabel("Date: ");
	lblDate.setForeground(Color.WHITE);
	lblDate.setBounds(25, 65, 238, 16);
	add(lblDate);

	distLabel = new JLabel();
	distLabel.setForeground(Color.WHITE);
	distLabel.setBounds(25, 93, 238, 16);
	add(distLabel);

	/*
	 * JLabel lblNewLabel = new JLabel("Distance Remaining: ");
	 * lblNewLabel.setForeground(Color.WHITE); lblNewLabel.setBounds(24,
	 * 121, 239, 16); add(lblNewLabel);
	 */

	/**
	 * This JLabel will display all the members and their current health
	 * status next to their names.
	 */
	/*
	 * JLabel lblPartyMembers = new JLabel("Party Members: ");
	 * lblPartyMembers.setForeground(Color.WHITE);
	 * lblPartyMembers.setBounds(25, 192, 238, 16); add(lblPartyMembers);
	 */

	/**
	 * Button to call for Trade with other travelers
	 */
	JButton btnTradeWithOther = new JButton("Trade with other Travelers");
	btnTradeWithOther.setBounds(6, 346, 196, 29);
	add(btnTradeWithOther);

	/**
	 * Button to call for Hunt
	 */
	JButton btnHunt = new JButton("Hunt");
	btnHunt.setBounds(6, 374, 117, 29);
	add(btnHunt);

	/**
	 * Button to call for Rest
	 */
	JButton btnRest = new JButton("Rest");
	btnRest.setBounds(6, 402, 117, 29);
	add(btnRest);

	/**
	 * Button to call for display Map
	 */
	btnMao = new JButton("Map");
	btnMao.setBounds(656, 362, 117, 29);
	add(btnMao);
	ButtonListener menuListener = new ButtonListener();
	btnMao.addActionListener(menuListener);

	JLabel lblInventory = new JLabel("Inventory");
	lblInventory.setForeground(Color.WHITE);
	lblInventory.setBounds(558, 65, 61, 16);
	add(lblInventory);

	JLabel lblNewLabel_1 = new JLabel();
	lblNewLabel_1.setText("Available Bankroll: " + nf.format(bankroll));
	lblNewLabel_1.setForeground(Color.WHITE);
	lblNewLabel_1.setBounds(558, 93, 215, 16);
	add(lblNewLabel_1);

	JLabel lblOxen = new JLabel("Oxen: ");
	lblOxen.setForeground(Color.WHITE);
	lblOxen.setBounds(558, 121, 215, 16);
	add(lblOxen);

	foodLeftLabel = new JLabel();
	foodLeftLabel.setForeground(Color.WHITE);
	foodLeftLabel.setBounds(558, 149, 215, 16);
	add(foodLeftLabel);

	JLabel lblNewLabel_2 = new JLabel("Spare Tongues:");
	lblNewLabel_2.setForeground(Color.WHITE);
	lblNewLabel_2.setBounds(558, 177, 215, 16);
	add(lblNewLabel_2);

	JLabel lblSpareAxels = new JLabel("Spare Axels:");
	lblSpareAxels.setForeground(Color.WHITE);
	lblSpareAxels.setBounds(558, 205, 215, 16);
	add(lblSpareAxels);

	JLabel lblSpareWheels = new JLabel("Spare Wheels:");
	lblSpareWheels.setForeground(Color.WHITE);
	lblSpareWheels.setBounds(558, 233, 215, 16);
	add(lblSpareWheels);

	JLabel lblClothes = new JLabel("Clothes: ");
	lblClothes.setForeground(Color.WHITE);
	lblClothes.setBounds(558, 261, 215, 16);
	add(lblClothes);

	JLabel lblBullets = new JLabel("Bullets:");
	lblBullets.setForeground(Color.WHITE);
	lblBullets.setBounds(558, 289, 215, 16);
	add(lblBullets);

	ActionListener continuelistener = new ButtonListener();
	;

	/**
	 * Continue Button
	 * 
	 * When this button is clicked, gameplay returns to the JourneyView
	 */
	btnContinue = new JButton("Continue");
	btnContinue.setBounds(380, 418, 117, 29);
	add(btnContinue);
	btnContinue.addActionListener(continuelistener);

	/**
	 * Pace Combo Box and Labels
	 */
	milesbox.setModel((new DefaultComboBoxModel(milesArray)));
	milesbox.setBounds(295, 173, 136, 27);
	add(milesbox);

	JLabel lblPace = new JLabel("PACE:");
	lblPace.setForeground(Color.WHITE);
	lblPace.setBounds(344, 142, 61, 16);
	add(lblPace);

	/**
	 * Ration Combo Box and Labels
	 */
	JLabel lblRation = new JLabel("RATION:");
	lblRation.setForeground(Color.WHITE);
	lblRation.setBounds(341, 65, 61, 16);
	add(lblRation);

	rationbox.setModel((new DefaultComboBoxModel(rationArray)));
	rationbox.setBounds(320, 89, 90, 27);
	add(rationbox);

	JLabel lblRatesday = new JLabel("Meals/Day");
	lblRatesday.setForeground(Color.WHITE);
	lblRatesday.setBounds(428, 93, 71, 16);
	add(lblRatesday);

	// add the background image
	JLabel image = new JLabel("");

	JButton btnBtnsave = new JButton("Save Game");
	btnBtnsave.setBounds(265, 14, 117, 29);
	add(btnBtnsave);

	btnBtnsave.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent arg0) {
			onSaveGameClick();
	    }
	});
	
	image.setIcon(new ImageIcon(
		StoreBGView.class
			.getResource("/edu/gatech/cs2340/bytebankers/graphics/MenuBGImg.png")));

	image.setBounds(0, 0, 860, 500);
	add(image);
	
	

    }

    public void setDistanceTravelled(double distance) {
	this.distance = distanceTravelled;
	distLabel.setText("Distance Travelled: " + distanceTravelled);

    }

    /**
     * Setter for amount of food left
     * 
     * @param int food left
     */
    public void setFoodLeft(int foodLeft) {
	this.foodLeft = foodLeft;
	foodLeftLabel.setText("Food: " + foodLeft);

    }

    /**
     * Setter for ration
     */
    public void setRation(int ration) {
	this.ration = ration;
	int rationIndex = getRationAmountIndex(((Integer) ration).toString());
	rationbox.setSelectedIndex(rationIndex);
    }

    /**
     * Setter for pace that takes in a string
     */
    public void setPace(String paceString) {
	this.paceString = paceString;
	int paceIndex = 0;
	if (paceString.toLowerCase().equals("very slow")) {
	    paceIndex = 0;
	    pace = 2;
	} else if (paceString.toLowerCase().equals("slow")) {
	    paceIndex = 1;
	    pace = 4;
	} else if (paceString.toLowerCase().equals("medium")) {
	    paceIndex = 2;
	    pace = 6;
	} else if (paceString.toLowerCase().equals("fast")) {
	    paceIndex = 3;
	    pace = 8;
	} else if (paceString.toLowerCase().equals("very fast")) {
	    paceIndex = 4;
	    pace = 10;
	}
	milesbox.setSelectedIndex(paceIndex);

    }

    /**
     * Setter for pace that takes in an int
     */
    public void setPace(int pace) {
	this.pace = pace;
	switch (pace) {
	case 2:
	    milesbox.setSelectedIndex(0);
	    break;
	case 4:
	    milesbox.setSelectedIndex(1);
	    break;
	case 6:
	    milesbox.setSelectedIndex(2);
	    break;
	case 8:
	    milesbox.setSelectedIndex(3);
	    break;
	case 10:
	    milesbox.setSelectedIndex(4);
	    break;
	}
    }

    /**
     * Closes the panel when the user clicks the Continue button
     */
    public void OnOkClick() {
	ration = Integer.parseInt(rationbox.getSelectedItem().toString());
	setPace(milesbox.getSelectedItem().toString());
	presenter.onOkClick();
    }

    /**
     * Setter for the current bankroll of the player
     */
    public void setBankroll(double bankroll) {
	this.bankroll = bankroll;
    }

    /**
     * Getter for ration
     */
    public int getRation() {
	return ration;
    }

    /**
     * Getter for pace
     */
    public int getPace() {
	return pace;
    }

    /**
     * Getter for panel
     */
    public JPanel getMainPanel() {
	return this;
    }

    /**
     * Setter for date
     */
    public void setDate(String date) {
	date.toString();
    }

    private int getRationAmountIndex(String rationValue) {
	int count = 0;
	for (String rationRate : rationArray) {
	    if (rationRate.equals(rationValue)) {
		return count;
	    }
	    count++;
	}

	return -1;
    }

    private int getPaceIndex(String paceValue) {
	int count = 0;
	for (String paceRate : milesArray) {
	    if (paceRate.equals(paceValue)) {
		return count;
	    }
	    count++;
	}

	return -1;
    }

    private class ButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent event) {
	    if (event.getSource() == btnContinue) {
		OnOkClick();
	    } else if (event.getSource() == btnMao) {
		onMapClick();
	    }
	}
    }

    public void onMapClick() {
	presenter.onMapClick();
    }

	@Override
	public void onSaveGameClick() {
		presenter.onSaveGameClick();
		
	}
}
